package com.example.r2022913.controller.WxController;


import com.alibaba.fastjson.JSONObject;
import com.example.r2022913.common.ApiRestResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//获取token的controller
@RestController
@RequestMapping("wx/login")
@Api(tags = "wx小程序服务  loginController")
@CrossOrigin
@Slf4j
public class LoginTokenController {

    //和微信小程序绑定的appid
    @Value("${appid}")
    private String appid;

    //和微信小程序绑定的secret
    @Value("${secret}")
    private String secret;

    @GetMapping("/token/os")
    @ApiOperation(value = "获取openid sessionKey的接口 需要前端传递code")
    public ApiRestResponse get_os(@RequestParam String code) throws IOException {

        log.info("appid是==》{}",appid);
        log.info("secret-->{}",secret);
        log.info("前端传递的code是===》{}",code);

        //使用OKhttpclient
        OkHttpClient okHttpClient = new OkHttpClient();

        //封装Url
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";

        //创建请求 import okhttp3.Request;
        Request request = new Request.Builder().url(url).build();

        //获取请求响应  import okhttp3.Response;
        Response response = okHttpClient.newCall(request).execute();

        log.info("发起请求后的响应是===》{}",response);

        //创建一个hashmap
        Map<String, String> map = new HashMap<>();

        //成功
        if (response.isSuccessful()) {

            //使用fastjson把response的字符串转化为json对象
            //方便后续使用
            JSONObject jsonResult = JSONObject.parseObject(response.body().string());

            log.info("获取的响应结果体是===》{}",jsonResult);

            //得到openid
            String openid = jsonResult.getString("openid");
            //得到session_key
            String sessionKey = jsonResult.getString("session_key");

            log.info("获取的openid是==》{}",openid);
            log.info("获取的sessionKey是==》{}",sessionKey);

            //把从response获取到的对象存入map中
            map.put("openid",openid);
            map.put("sessionKey",sessionKey);

            String token = "token123";


            return ApiRestResponse.success(token);
        }
        else {
            return  ApiRestResponse.error(405,"失败获取token");
        }

    }



    @GetMapping("/token/access_token")
    @ApiOperation(value = "获取access_token的接口")
    public ApiRestResponse get_Access_token() throws IOException {

        log.info("appid 是==》{}",appid);
        log.info("secret -->{}",secret);

        //使用OKhttpclient
        OkHttpClient okHttpClient = new OkHttpClient();

        //封装Url
        String url = "https://api.weixin.qq.com/cgi-bin/token?appid=" + appid + "&secret=" + secret +  "&grant_type=client_credential";

        //创建请求 import okhttp3.Request;
        Request request = new Request.Builder().url(url).build();

        //获取请求响应  import okhttp3.Response;
        Response response = okHttpClient.newCall(request).execute();

        log.info("发起请求后的响应是===》{}",response);

        //创建一个hashmap
//        Map<String, String> map = new HashMap<>();

        //成功
        if (response.isSuccessful()) {

            //使用fastjson把response的字符串转化为json对象
            //方便后续使用
            JSONObject jsonResult = JSONObject.parseObject(response.body().string());

            log.info("获取的响应结果体是===》{}",jsonResult);

            //得到access_token
            String access_token = jsonResult.getString("access_token");
            log.info("获取到的access_token是===>{}",access_token);




            return ApiRestResponse.success();
        }
        else {
            return  ApiRestResponse.error(405,"失败获取access_token");
        }

    }

}
